package com.ar4j.pagination;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * Simple implementation of a sort field with a field name and an optional descending sort
 */
public class BasicSortField implements ISortField {
  private String fieldName;
  private boolean isDescending;
  
  /**
   * Construct a new sort field with a field name and ascending sort order
   */
  public BasicSortField(String fieldName) {
    this.fieldName = fieldName;
  }
  
  /**
   * Construct a new sort field with a field name and an optionally descending sort order
   */
  public BasicSortField(String fieldName, boolean isDescending) {
    this(fieldName);
    this.isDescending = isDescending;    
  }
  
  @Override
  public String getFieldName() {
    return fieldName;
  }
    
  @Override
  public SortDirection getSortDirection() {
    return (isDescending ? SortDirection.DESCENDING : SortDirection.ASCENDING);
  }
  
  @Override
  public String toString() {
    return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
  }
}
